package org.ricks.grids;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ricks [chenwei]
 * @Version [v1.0]
 * @CreateTime 2024-06-24   15:57
 * @Description TODO
 */
public class HexUtils {
    /*
     * 这里的格子内径、外径是对应Unity的。
     * 在逻辑计算时，统一使用按照1的距离处理。
     * 这个参数的设置，只会影响对Unity坐标的转换；
     *
     * ——程一峰；2022.05.27
     */

    /// <summary>
    /// HexGrid的内径长度
    /// 中心点到边的距离
    /// </summary>
    public static float HexGridInnerRadius  = 1;

    /// <summary>
    /// 内径/外径  3^0.5 / 4
    /// </summary>
    public static float InnerToOutter = 0.8660254037844386f;

    /// <summary>
    /// 外径/内径  4 / 3^0.5
    /// </summary>
    public static float OutterToInner = 1.154700538379252f;

    /// <summary>
    /// HexGrid的外径长度
    /// </summary>
    public static float HexGridOuterRadius  = OutterToInner;

    /// <summary>
    /// 列间距（内径的2倍）；
    /// </summary>
    public static float ColumnLength  = 2;

    /// <summary>
    /// 行间距(外径的1.5f倍)
    /// </summary>
    public static float RowLength = 1.5f * HexGridOuterRadius;


    public static void InitSize(float InnerRadius)
    {
        HexGridInnerRadius = InnerRadius;
        HexGridOuterRadius = HexGridInnerRadius * OutterToInner;
        ColumnLength = HexGridInnerRadius * 2.0f;
        RowLength = HexGridOuterRadius * 1.5f;
    }


    /// <summary>
    /// 对应 E_HexDirection 的各个方向的六边形坐标偏移；
    /// </summary>
    public static Hex[] HexDirOffset = new Hex[]
    {
        new Hex(0,0,0),//原点
                new Hex(1,-1,0),//正右
                new Hex(0,-1,1),//右上
                new Hex(-1,0,1),//左上
                new Hex(-1,1,0),//正左
                new Hex(0,1,-1),//左下
                new Hex(1,0,-1),//右下
    };

    /// <summary>
    /// 根号3；
    /// </summary>
    public static float Sqrt_3 = 1.73205080757f;





    private static List<Hex> mTempList_TileCoordinate = new ArrayList<>();

    /// <summary>
    /// 获取半径范围内的所有格子坐标；
    /// </summary>
    /// <param name="center"></param>
    /// <param name="range"></param>
    /// <param name="resultL"></param>
    public static void GetRangeGrid(Hex hexGrid, int range, List<Hex> resultL)
    {
        if (resultL == null)
            resultL = new ArrayList<>();
        else
            resultL.clear();
        //根据范围进行搜索；
        for (int i = 0; i <= range; i++)
        {
            FindCircleGrid(hexGrid, i, mTempList_TileCoordinate);
            resultL.addAll(mTempList_TileCoordinate);
        }
        mTempList_TileCoordinate.clear();
    }

    /// <summary>
    /// 获取环形格子的坐标（一圈）；
    /// </summary>
    /// <param name="centerPos">中心点</param>
    /// <param name="radius">半径</param>
    /// <param name="resultL">返回结果</param>
    public static void FindCircleGrid(Hex centerPos, int radius, List<Hex> resultL)
    {
        if (resultL == null)
            resultL = new ArrayList<>();
        else
            resultL.clear();

        if (radius == 0)
        {
            //肯定包括自己；
            resultL.add(centerPos);
            return;
        }

        //1、往左下移动固定长度，作为起点；
        var pos = HexGridMoveNext(centerPos, E_HexDirection.LeftDown, radius);
        int slideLength = radius;

        //2、//绕着转一圈；
        for (int i = (int)E_HexDirection.Right; i <= (int)E_HexDirection.RightDown; i++)
        {
            //3、范围越大，单个边的长度越长；
            for (int j = 0; j < slideLength; j++)
            {
                //进行移动；
                pos = HexGridMoveNext(pos, i, 1);
                //加进列表；
                resultL.add(pos);
            }
        }
    }

    public static Hex ToInt3( HexGrid grid) { return new Hex(grid.x, grid.y, grid.z); }


    /// <summary>
    /// 按照步长和方向进行移动
    /// </summary>
    /// <param name="from">HexGrid 坐标</param>
    /// <param name="dri">移动方向</param>
    /// <param name="stepLength">步长</param>
    /// <returns>返回 HexGrid 坐标</returns>
    public static Hex HexGridMoveNext( Hex from, int dri, int stepLength)
    {
        if (dri == E_HexDirection.None)
            return from;
        for (int i = 0; i < stepLength ; i++) {
            from = from.neighbor(dri);
        }
        return from;
    }


    public static void main(String[] args) {
        List<Hex> hexes = new ArrayList<>();
        HexUtils.GetRangeGrid(new Hex(0,0,0), 4, hexes);
//        for (int i = 0; i < hexes.size() ; i++) {
//            System.err.println("id = "+ i + " xyz:" + hexes.get(i).toString());
//        }

        System.err.println(hexes.size());

        Hex markHex = hexes.get(18);

        boolean check = List.of(0, 1, 2, 3, 4, 5, 6, 9, 7, 15, 13, 17, 16, 18, 34, 35, 55, 32, 33, 59, 58, 36, 37, 60, 19, 56, 57, 20, 39, 38, 29, 28, 14, 54, 31, 53, 52, 30, 22, 8, 23, 50, 49, 51, 42, 43, 41).stream().anyMatch(landId -> {
            Hex hex =  hexes.get(landId);
            return markHex.neighbor(hex);
        });
        System.err.println("是否相邻。。。。 check:" + check);
    }

    public static boolean equalHex(Hex a, Hex b) {
        return a.q == b.q && a.s == b.s && a.r == b.r;
    }
}
